Skip to content

fix(electron-chrome-extensions): buffer native messaging host output across stdout chunks - #191

Open
mutewinter wants to merge 1 commit into
samuelmaddock:masterfrom
mutewinter:fix/native-messaging-chunked-stdout
Open

mutewinter wants to merge 1 commit into
samuelmaddock:masterfrom
mutewinter:fix/native-messaging-chunked-stdout

Conversation

@mutewinter

Copy link
Copy Markdown

Problem

A native messaging host whose reply is larger than one stdout read crashes the main process's handler, and the extension never gets the message. With the 1Password desktop app connected to its extension, NmRequestAccounts returns more than 64 KB, and main logs two uncaught exceptions back to back:

SyntaxError: Unterminated string in JSON at position 65532 (line 1 column 65533)
  at JSON.parse (<anonymous>)
  at NativeMessagingHost.receive (electron-chrome-extensions/dist/esm/index.mjs:1817:28)
  at Socket.emit (node:events:514:28)
SyntaxError: Unexpected non-whitespace character after JSON at position 3 (line 1 column 4)
  at JSON.parse (<anonymous>)
  at NativeMessagingHost.receive (electron-chrome-extensions/dist/esm/index.mjs:1817:28)

The first is the first 64 KB chunk (65536 minus the 4-byte header). The second is the rest of that message read as a new one. To the user, 1Password unlocks but its extension has no accounts, which looks like an extension bug, not a framing bug.

Cause

receive is the stdout data handler, and it assumes one chunk is one message:

private receive = (data: Buffer) => {
  const length = data.readUInt32LE(0)
  const message = JSON.parse(data.subarray(4, 4 + length).toString())

Pipe reads give no guarantee about message boundaries. A message larger than the pipe's buffer arrives over several chunks, and two small messages written close together can arrive in one chunk, where everything after the first is silently dropped. The existing specs echo small values, which always fit in one chunk.

Fix

receive adds each chunk to a buffer and parses only complete length-prefixed messages, handling any number per chunk and keeping the leftover for the next read. A message that fails to parse is logged through debug and skipped, so it can't throw out of a stream handler.

Scope

Unchanged: the message format, send, and the keepAlive and one-shot response paths.

Not addressed, deliberately: send sizes its buffer with message.length (UTF-16 code units) rather than Buffer.byteLength(message), so a payload with non-ASCII characters is cut short and its length prefix is wrong. That's a separate bug, and I can send a one-line fix for it.

Tests

Added receives a message larger than one stdout chunk to chrome-nativeMessaging-spec.ts: it echoes a 256 KB object through runtime.sendNativeMessage. The test host read stdin with a single readSync, which can return less than a whole large message from a pipe, so it now loops until it has the full length.

On macOS with Electron 44, node ./script/spec-runner.js --grep nativeMessaging:

  • With this change: 3 passing, 0 failing.
  • With only native-messaging-host.ts reverted to master (same spec and test host): the new spec fails with SyntaxError: Unterminated string in JSON at position 65532, followed by uncaught Unexpected token 'x' errors from the leftover chunks. Verified by reverting and re-running, not inferred.

Only the native messaging specs were run, not the full suite. Like the existing native messaging specs, the new one is skipped on Linux. The same change, patched into 4.10.0, ships in a production Electron app where the 1Password errors above came from.

This PR was written with an AI coding agent (Claude) and reviewed by me before submitting.


Authored with Claude Opus 5.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant